<admintpl file="header"/>
<style>
	.span3{
		position: relative;
		width:572px;
	}
	.span3 .add{
		position:absolute;right:-70px;top:10px;
	}
	.span3 input{
		margin-bottom:15px;
	}
	.searchText{
		width:528px;height:auto;padding:20px;border: 2px solid #dce4ec;position:absolute;top:36px;background: #fff;border-top:none;
		display: none; z-index: 10px;
	}
	.searchText span{
		display: block;
		line-height:24px;
		color:#999;
		cursor:pointer;
	}
	.searchText span:hover{
		background: #2ea7e0;
		color:#fff;
	}
	.remove{
		position: relative;
	}
	.remove .del{
		position:absolute;right:-56px;top:10px;
	}

</style>
</head>

<body>
<div class="wrap">
    <ul class="nav nav-tabs">
        <li><a href="{:U('Admin/News/index')}">新闻列表</a></li>
        <li class="active"><a href="{:U('Admin/News/add')}">添加{$Title}</a></li>
    </ul>
    <form method="post" class="form-horizontal" id="add_area" onsubmit="add_area();"
          action="{:U('Admin/Area/areaPost')}">
        <div class="control-group">
            <label class="span1 control-label" style="width: 90px;">选择地区</label>
            <div class="span3">
                <select name="province" id="province" onchange="show_city(this.options[this.options.selectedIndex].value)">
					<!--show_city(this.options[this.options.selectedIndex].value)-->
					<option value="">请选择</option>
					<foreach name="province_list" item="vo" key="key">
						<option value="{$vo.provinceid}">{$vo.province}</option>
					</foreach>
				</select>
				<div style="height: 10px;"></div>
				<select name="city" id="city">
					<option value="">请选择</option>;
				</select>
            </div>
        </div>

         <div class="control-group">
            <label class="span1 control-label" style="width: 90px;">公司名称</label>
            <div class="span3">
                <input type="text"  style="width: 556px;" name="company_name" id="company_name" >
            </div>
        </div>
		<div class="control-group">
			<label class="span1 control-label" style="width: 90px;">公司简称</label>
			<div class="span3">
				<input type="text"  style="width: 556px;" name="ename" id="ename" >
			</div>
		</div>
		<div class="control-group">
			<label class="span1 control-label" style="width: 90px;">设置管理员</label>
			<div class="span3" id="admin" >
				<input type="text"  style="width: 556px;" name="adminstract[]" id="adminstract0" class="admin" onkeyup="find_admin(0)" value=""><a class="add" style="font-size: 20px;margin-left: 10px;" onclick="copyHtml1()">添加+</a>
				<div class="searchText" id="searchText0">

				</div>
			</div>

		</div>
        <!--<div class="control-group">-->
            <!--<label class="span1 control-label" style="width: 90px;"></label>-->
            <!--<div class="span3">-->
                <!--<foreach name="administrator_data" key="administrator_key" item="administrator_value">-->
                <!--<input name="administrator_checbox"  administrator_name="{$administrator_value}"  type="checkbox"   value="{$administrator_key}"  />{$administrator_value}&nbsp;&nbsp;&nbsp;-->
                <!--</foreach>-->
            <!--</div>-->
        <!--</div>-->
        
        <input type="hidden" name="administrator_hidden_ids" id="administrator_hidden_ids" value="">
		<input type="hidden" name="area_hidden_ids" id="area_hidden_ids" value="">
        <div class="form-actions" style="padding-left:260px;">
            <button type="button" class="btn btn-primary " onclick="add_area();">保存</button>
            &nbsp;&nbsp;<a class="btn" href="{:U('Admin/Area/index')}">{:L('BACK')}</a>
        </div>
    </form>

</div>
<script src="__PUBLIC__/js/common.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/content_addtop.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/ueditor/ueditor.config.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/ueditor/ueditor.all.min.js"></script>
<script type="text/javascript"
        src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.min.js"></script>
<script>

	function disable_div(num) {
		//$("#searchText"+num).css('display', 'none');
	}
	function find_admin(num){
		var find = $("#adminstract"+num).val();
		if(find){
			$.ajax({
				url:"{:U('Admin/Area/findAdmin')}",
				type:"post",
				data: {find:find},
				async: false,
				success:function(response){
					if(response){
						$("#searchText"+num).css('display', 'block');
						var data = JSON.parse(response);
						var html = '';
						for(var i=0;i<data.length;i++){
							html += '<span class="search-email" data-num="' + num + '" data-index="' + i + '" id="find'+i+'">'+data[i]['username']+'</span>';
						}
						$("#searchText"+num).html(html);
					}
				}
			})
		}
	}
	function add_area() {
		var city = $('#city option:selected') .val();//选中的值
		var province = $("#province").find("option:selected").text();//选中的值
		var company_name = $("#company_name").val();
		var ename = $("#ename").val();
		var adminstract = $("[name='adminstract[]']");
		var adminstract_arr = [];
		for(i=0;i<adminstract.length;i++){
			adminstract_arr.push(adminstract[i].value);
		}
		var flag = true;
		if(!city || !province || !company_name || !ename){
			alert('地区、公司名称、公司简称为必填项！');
			flag = false;
			return false;
		}
		if(city){
			$.ajax({
				url:"{:U('Admin/Area/checkCity')}",
				type:"post",
				data: {city:city},
				async: false,
				success:function(response){
					if(response == 1){
						alert('该公司已存在');
						flag = false;
						return false;
					}else{
						flag = true;
						return true;
					}
				}
			})
		}
		if(flag == false){
			return flag;
		}
		$.ajax({
			url:"{:U('Admin/Area/areaPost')}",
			type:"post",
			data: {city:city,company_name:company_name,ename:ename,adminstract_arr:adminstract_arr,province:province},
			success:function(response){
				if(response == 1){
					alert('添加成功！');
					window.location.href = "{:U('Admin/Area/index')}";
				}else if(response == -1){
					alert('地区、公司名称、公司简称为必填项！');
				}else{
					alert('添加失败！');
				}
			}
		})

	}

	function copyHtml1(){
		var num = $('.searchText').length;
		var str = '<div class="remove"><input type="text"  style="width: 556px;" name="adminstract[]" id="adminstract'+num+'" onkeyup="find_admin('+num+')" onblur="disable_div('+num+')">';
		str += '<a style="font-size: 20px;padding-left: 10px;" onclick="removeHtml1(this)"  class="del">删除</a><div id="searchText'+num+'" class="searchText"></div></div>';
		$('#admin').append(str);
	}

	function removeHtml1(dom) {
		$(dom).parent().remove();
	}
	function show_city(id){
		$.ajax({
			url:"{:U('Admin/Area/getCityList')}",
			type:"post",
			data: {provinceId:id},
			success:function(response){
				if(response){
					var data = JSON.parse(response);
					if(data.length > 0){
						var html = '<option value="">请选择</option>';
						for(var i=0;i<data.length;i++){
							html += '<option value="'+data[i]['city']+'">'+data[i]['city']+'</option>';
						}
						$('#city').html(html);
					}
				}
			}
		})
	}
$(function(){
	$("[name='area_checbox']").click(function(){
		var area_id = $(this).val();
		var area_name = $(this).attr('area_name');
		
		var area_hidden_ids = $("#area_hidden_ids").val();
		var area_values = $("#area").val();
		
		var ids_arr = area_hidden_ids.split(",");          
		var values_arr = area_values.split(",");   
		
		if(ids_arr.indexOf(area_id) == '-1'){
			area_hidden_ids+=area_id+',';
			area_values+=area_name+',';
			$("#area").val(area_values);
			$("#area_hidden_ids").val(area_hidden_ids);
		}else{
			$.each(ids_arr,function(index,item){
				 if(item==area_id){
					 ids_arr.splice(index,1);
					 values_arr.splice(index,1);
				 }
			}) 
			area_hidden_ids = ids_arr.join(",");
			area_values = values_arr.join(",");
			$("#area").val(area_values);
			$("#area_hidden_ids").val(area_hidden_ids);
		}
	})
	
	$("[name='administrator_checbox']").click(function(){
		var administrator_id = $(this).val();
		var administrator_name = $(this).attr('administrator_name');
		
		var administrator_hidden_ids = $("#administrator_hidden_ids").val();
		var administrator_values = $("#administrator").val();
		
		var ids_arr = administrator_hidden_ids.split(",");         
		var values_arr = administrator_values.split(",");

		if(ids_arr.indexOf(administrator_id) == '-1'){
			administrator_hidden_ids+=administrator_id+',';
			administrator_values+=administrator_name+',';
			$("#administrator").val(administrator_values);
			$("#administrator_hidden_ids").val(administrator_hidden_ids);
		}else{
			$.each(ids_arr,function(index,item){
				 if(item==administrator_id){
					 ids_arr.splice(index,1);
					 values_arr.splice(index,1);
				 }
			}) 
			administrator_hidden_ids = ids_arr.join(",");
			administrator_values = values_arr.join(",");
			$("#administrator").val(administrator_values);
			$("#administrator_hidden_ids").val(administrator_hidden_ids);
		}
	})

	$("body").on("click", ".search-email", function (e) {
		e.stopPropagation();
		var val = $(this).text();
		var number = $(this).data("num");
//		$(this).parent().hide().siblings("input").val(val);
		$("#adminstract"+number).val(val);
		$("#searchText"+number).hide();
	});
	$("body").on("click", function () {
		$(".searchText").hide()
	});

});
	function search_name(num, number){
		console.log(num)
		var val = $("#find"+num).text();
		$("#adminstract"+number).val(val);
		$("#searchText"+number).css('display', 'none');
	}
</script>


</body>
</html>